package com.canbot.u05.activity.customvoice.adapter

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.canbot.u05.R
import com.canbot.u05.activity.customvoice.bean.AnswerBean
import com.canbot.u05.glide.GlideApp
import com.canbot.u05.socket.IpMsgConst
import kotlinx.android.synthetic.main.custom_dialog_datails_item.view.*

/**
 * CustomDialogDetailsAdapter
 *
 * @author  Liu.Heng
 * @date  2019/6/4
 */
class CustomDialogDetailsAdapter(val context: Context, var list: MutableList<AnswerBean>) : RecyclerView.Adapter<CustomDialogDetailsAdapter.MyViewHolder>() {

    private val layoutInflater: LayoutInflater = LayoutInflater.from(context)

    private var mList = list

    override fun onCreateViewHolder(p0: ViewGroup, p1: Int): MyViewHolder {
        val inflate = layoutInflater.inflate(R.layout.custom_dialog_datails_item, p0, false)
        return MyViewHolder(inflate)
    }

    override fun getItemCount(): Int {
        return mList.size
    }

    override fun onBindViewHolder(myViewHolder: MyViewHolder, position: Int) {
        val imageView1 = myViewHolder.imageView1
        val imageView2 = myViewHolder.imageView2
        val myTextView1 = myViewHolder.myTextView1
        val myTextView2 = myViewHolder.myTextView2
        val myTextView3 = myViewHolder.myTextView3
        val myTextView4 = myViewHolder.myTextView4

        val answerBean = list[position]
        if (answerBean.imgpath.isNotEmpty()) {
            myTextView3.text = "一张图片"
            GlideApp.with(context).clear(imageView1)
            val requestOptions = RequestOptions.circleCropTransform()
            GlideApp.with(context)
                    .load(IpMsgConst.HTTP_URL_CHEST + answerBean.imgpath.replace("/uupic/", "/uupic_thumb/"))
                    .thumbnail(0.1f)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)//缓存策略
                    .skipMemoryCache(false)
                    .placeholder(R.drawable.custom_dialog2_answer_pic_moren)//占位图
                    .apply(requestOptions).into(imageView1)
        } else {
            GlideApp.with(context).clear(imageView1)
            GlideApp.with(context).load(R.drawable.custom_dialog2_answer_item_add).into(imageView1)
            myTextView3.text = "添加图片"
        }
        if (answerBean.actionid.isNotEmpty()) {
            myTextView4.text = "一个动作"
            imageView2.background = context.getDrawable(R.drawable.custom_dialog2_answer_act)
        }
        if (answerBean.type == 1) {
            myTextView1.text = "说一段话"
            myTextView2.text = answerBean.ttstext
            myTextView2.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null)
        } else {
            myTextView1.text = "播放音频"
            myTextView2.text = ""
            myTextView2.setCompoundDrawablesWithIntrinsicBounds(context.getDrawable(R.drawable.custom_dialog2_answer_voc), null, null, null)
        }
    }

    class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var imageView1: ImageView = view.pic_imageView
        var imageView2: ImageView = view.action_imageView
        var myTextView1: TextView = view.my_text1
        var myTextView2: TextView = view.my_text2
        var myTextView3: TextView = view.pic_text
        var myTextView4: TextView = view.action_text

    }
}